<?php

require_once('_Core.php');

function phptal_tales_link($pSrc, $pNoThrow){
    $src = trim($pSrc);
    return '$ctx->this->objectRender(array(\'Type.slug\' => \'link\', \'slug\' => '.PHPTAL_Php_TalesInternal::string($src, $pNoThrow).'), \'view\')';
}

// class SilkCore_LinkMeta_Url extends Silk_Module_Type_Meta {
//
//     public function getName(){
// 	return 'Url';
//     }
//
//     public function getInputType(){
// 	return Silk::INPUT_TEXT;
//     }
//
// }
//
// class SilkCore_LinkMeta_Target extends Silk_Module_Type_Meta {
//
//     public function getName(){
// 	return 'Link Target';
//     }
//
//     public function getInputType(){
// 	return Silk::INPUT_TEXT;
//     }
//
// }
//
// class SilkCore_LinkMeta_Image extends Silk_Module_Type_Meta {
//
//     public function getName(){
// 	return 'Image';
//     }
//
//     public function getInputType(){
// 	return Silk::INPUT_SELECT;
//     }
//
//     public function getValues(){
// 	return '@Type=Image';
//
//     }
// }


class SilkCore_Type_Link extends SilkCore_Type_Base {

//     public function __construct(){
// 	$this->_meta = array(
// 	    new SilkCore_LinkMeta_Url(),
// 	    new SilkCore_LinkMeta_Target(),
// 	    new SilkCore_LinkMeta_Image()
// 	);
//     }

    public function getName(){
	return 'Link';
    }

    public function getSlug(){
	return 'link';
    }

    public function getDescription(){
	return 'Managed Link';
    }

    public function render(Silk_Object $pLink=null, $pAction=null, array $pVars=array()){
	logdebug('SilkCore_Type_Link.render: Called');
	$view = $this->prepare($pLink, $pVars, $pAction);

	if ($pAction instanceof Silk_Template)
	    return parent::render($pLink, $pAction, $pVars);
	else if ($pAction){
	    logdebug('SilkCore_Type_Link.render: Rendering action ' . $pAction);
	    $action = strtolower($pAction);
	    switch ($action){
		case 'admin':
		    return $this->_admin($view);
		case 'create':
		    return $this->_create($view, $pLink);
		case 'view':
		    return $this->_view($view, $pLink);
		case 'edit':
		    return $this->_edit($view, $pLink);
		case 'delete':
		    return $this->_delete($view, $pLink);
		default:
		    return '<div class="silk-core-error">Action [' . $pAction . '] not defined</div>';
	    }

	} else {
	    return $this->renderString($view, $pLink->content);
	}
    }

    protected function prepare(Silk_Object $pLink=null, array $pVars=array(), $pAction=null){
	$view = $this->getViewEngine();

	if ($pLink){
	    $view->assign('link', $pLink);
	    $view->assign('object', $pLink);
	}


	foreach ($pVars as $key => $val){
	    $view->assign($key, $val);
	}

	return $view;
    }

    public function _view(Zend_View_Interface $pView, Silk_Object $pLink){

	$label = $pLink->getMetaValue('Label');

	if (!$label) $label = $pLink->name;

	return '<a href="' . $pLink->content . '">'. $label. '</a>';
    }

    protected function _create(Zend_View_Interface $pView, Silk_Object $pLink){
	logdebug('SilkCore_Type_Link._craeate: Rendering template edit-link');
	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	$pView->this->appendScript($this, 'js/core.manage.js');
	return $this->renderTemplate($pView, 'edit-link');

    }

     protected function _edit(Zend_View_Interface $pView, Silk_Object $pLink){

	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	$pView->this->appendScript($this, 'js/core.manage.js');
	return $this->renderTemplate($pView, 'edit-link');

    }

    protected function _admin(Zend_View_Interface $pView){
	if (array_key_exists('sort', $_REQUEST)){
	    $sort = $_REQUEST['sort'];
	} else
	    $sort = 'object_id';

	if (array_key_exists('order', $_REQUEST)){
	    $order = $_REQUEST['order'];
	} else
	    $order = 'ASC';

	if (array_key_exists('limit', $_REQUEST)){
	    $limit = $_REQUEST['limit'];
	} else
	    $limit = 50;

	if (array_key_exists('page', $_REQUEST)){
	    $page = $_REQUEST['page'];
	} else
	    $page = 1;

	$paginator = Silk::getObjectPaginator($this->getSlug(), array(), $sort, $order);

	$paginator->setItemCountPerPage($limit);
	$paginator->setCurrentPageNumber($page);

	$Types = new Silk_Types();
	$type = $Types->fetchBySlug($this->getSlug(), true);

	$pView->moduletype = $this;
	$pView->type = $type;

	$pView->links = $paginator;
	$pView->limit = $limit;
	$pView->sort  = $sort;
	$pView->order = $order;
	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	return $this->renderTemplate($pView, 'link-admin');
    }
}

?>